package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;

import object.Company;
import objectView.DepartmentCom;
import clientMain.Main;
import clientUtilGUI.KeyValue;
import clientUtilGUI.UtilityGUI;

public class Company_View extends JScrollPane{

	private JList jListDep;

	public Company_View(){
		Company com = Main.viewCompany.getCompany(Main.comId);
		
		Vector<DepartmentCom> listDep = Main.viewDep.getListDepInfo(Main.comId, 0, "Dep_Id", true);
		
		setViewportView(initComponet(com, listDep));
		setBorder(new EmptyBorder(0, 0, 0, 0));
	}
	
	/**
	 * initComponet
	 * @param Company com
	 * @param Vector<DepartmentCom> listDep
	 */
	public JPanel initComponet(Company com, Vector<DepartmentCom> listDep) {
		JPanel panel = new JPanel();
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		panel.setLayout(gridBagLayout);
		
		JLabel lblHeader = new JLabel(com.getCompanyName());
		lblHeader.setFont(UtilityGUI.fontHeader1);
		lblHeader.setForeground(UtilityGUI.colorHeader1);
		GridBagConstraints gbc_lblHeader = new GridBagConstraints();
		gbc_lblHeader.gridwidth = 3;
		gbc_lblHeader.insets = new Insets(10, 10, 15, 10);
		gbc_lblHeader.gridx = 0;
		gbc_lblHeader.gridy = 0;
		panel.add(lblHeader, gbc_lblHeader);
		
		JLabel lblCom = new JLabel("Công Ty :");
		lblCom.setFont(UtilityGUI.fontHeader2);
		GridBagConstraints gbc_lblCom = new GridBagConstraints();
		gbc_lblCom.anchor = GridBagConstraints.WEST;
		gbc_lblCom.insets = new Insets(0, 25, 10, 5);
		gbc_lblCom.gridx = 0;
		gbc_lblCom.gridy = 1;
		panel.add(lblCom, gbc_lblCom);
		
		JLabel lblComFullName = new JLabel(com.getCompanyName());
		lblComFullName.setFont(UtilityGUI.fontHeader2);
		lblComFullName.setForeground(UtilityGUI.colorHeader2);
		GridBagConstraints gbc_lblComFullName = new GridBagConstraints();
		gbc_lblComFullName.gridwidth = 2;
		gbc_lblComFullName.anchor = GridBagConstraints.WEST;
		gbc_lblComFullName.insets = new Insets(0, 5, 10, 10);
		gbc_lblComFullName.gridx = 1;
		gbc_lblComFullName.gridy = 1;
		panel.add(lblComFullName, gbc_lblComFullName);
		
		JLabel lblComType = new JLabel("Loại Hình :");
		lblComType.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblComType = new GridBagConstraints();
		gbc_lblComType.anchor = GridBagConstraints.WEST;
		gbc_lblComType.insets = new Insets(0, 25, 10, 5);
		gbc_lblComType.gridx = 0;
		gbc_lblComType.gridy = 2;
		panel.add(lblComType, gbc_lblComType);
		
		JLabel lblTypeName = new JLabel(com.getCompanyType());
		lblTypeName.setFont(UtilityGUI.fontLblForm);
		lblTypeName.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblTypeName = new GridBagConstraints();
		gbc_lblTypeName.anchor = GridBagConstraints.WEST;
		gbc_lblTypeName.insets = new Insets(0, 5, 10, 5);
		gbc_lblTypeName.gridx = 1;
		gbc_lblTypeName.gridy = 2;
		panel.add(lblTypeName, gbc_lblTypeName);
		
		JLabel lblComIndustry = new JLabel("Lĩnh Vực :");
		lblComIndustry.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblComIndustry = new GridBagConstraints();
		gbc_lblComIndustry.anchor = GridBagConstraints.WEST;
		gbc_lblComIndustry.insets = new Insets(0, 25, 10, 5);
		gbc_lblComIndustry.gridx = 0;
		gbc_lblComIndustry.gridy = 3;
		panel.add(lblComIndustry, gbc_lblComIndustry);
		
		JLabel lblIndustryName = new JLabel(com.getCompanyIndustry());
		lblIndustryName.setFont(UtilityGUI.fontLblForm);
		lblIndustryName.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblIndustryName = new GridBagConstraints();
		gbc_lblIndustryName.anchor = GridBagConstraints.WEST;
		gbc_lblIndustryName.insets = new Insets(0, 5, 10, 5);
		gbc_lblIndustryName.gridx = 1;
		gbc_lblIndustryName.gridy = 3;
		panel.add(lblIndustryName, gbc_lblIndustryName);
		
		JLabel lblFounder = new JLabel("Người sáng lập :");
		lblFounder.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblFounder = new GridBagConstraints();
		gbc_lblFounder.anchor = GridBagConstraints.WEST;
		gbc_lblFounder.insets = new Insets(0, 25, 10, 5);
		gbc_lblFounder.gridx = 0;
		gbc_lblFounder.gridy = 4;
		panel.add(lblFounder, gbc_lblFounder);
		
		JLabel lblFounderName = new JLabel(com.getCompanyFounder());
		lblFounderName.setFont(UtilityGUI.fontLblForm);
		lblFounderName.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblFounderName = new GridBagConstraints();
		gbc_lblFounderName.anchor = GridBagConstraints.WEST;
		gbc_lblFounderName.insets = new Insets(0, 5, 10, 5);
		gbc_lblFounderName.gridx = 1;
		gbc_lblFounderName.gridy = 4;
		panel.add(lblFounderName, gbc_lblFounderName);
		
		JLabel lblFounded = new JLabel("Thời điểm thành lập :");
		lblFounded.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblFounded = new GridBagConstraints();
		gbc_lblFounded.anchor = GridBagConstraints.WEST;
		gbc_lblFounded.insets = new Insets(0, 25, 10, 5);
		gbc_lblFounded.gridx = 0;
		gbc_lblFounded.gridy = 5;
		panel.add(lblFounded, gbc_lblFounded);
		
		JLabel lblFoundedDate = new JLabel(com.getCompanyFounded());
		lblFoundedDate.setFont(UtilityGUI.fontLblForm);
		lblFoundedDate.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblFoundedDate = new GridBagConstraints();
		gbc_lblFoundedDate.anchor = GridBagConstraints.WEST;
		gbc_lblFoundedDate.insets = new Insets(0, 5, 10, 5);
		gbc_lblFoundedDate.gridx = 1;
		gbc_lblFoundedDate.gridy = 5;
		panel.add(lblFoundedDate, gbc_lblFoundedDate);
		
		JLabel lblAddress = new JLabel("Địa Chỉ :");
		lblAddress.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblAddress = new GridBagConstraints();
		gbc_lblAddress.anchor = GridBagConstraints.WEST;
		gbc_lblAddress.insets = new Insets(0, 25, 10, 5);
		gbc_lblAddress.gridx = 0;
		gbc_lblAddress.gridy = 6;
		panel.add(lblAddress, gbc_lblAddress);
		
		JLabel lblAdd = new JLabel(com.getAddress());
		lblAdd.setFont(UtilityGUI.fontLblForm);
		lblAdd.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblAdd = new GridBagConstraints();
		gbc_lblAdd.gridwidth = 2;
		gbc_lblAdd.anchor = GridBagConstraints.WEST;
		gbc_lblAdd.insets = new Insets(0, 5, 10, 10);
		gbc_lblAdd.gridx = 1;
		gbc_lblAdd.gridy = 6;
		panel.add(lblAdd, gbc_lblAdd);
		
		JLabel lblPhone = new JLabel("Điện thoại :");
		lblPhone.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblPhone = new GridBagConstraints();
		gbc_lblPhone.anchor = GridBagConstraints.WEST;
		gbc_lblPhone.insets = new Insets(0, 25, 10, 5);
		gbc_lblPhone.gridx = 0;
		gbc_lblPhone.gridy = 7;
		panel.add(lblPhone, gbc_lblPhone);
		
		JLabel lblPhoneNum = new JLabel(com.getCompanyPhone());
		lblPhoneNum.setFont(UtilityGUI.fontLblForm);
		lblPhoneNum.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblPhoneNum = new GridBagConstraints();
		gbc_lblPhoneNum.anchor = GridBagConstraints.WEST;
		gbc_lblPhoneNum.insets = new Insets(0, 5, 10, 5);
		gbc_lblPhoneNum.gridx = 1;
		gbc_lblPhoneNum.gridy = 7;
		panel.add(lblPhoneNum, gbc_lblPhoneNum);
		
		JLabel lblWebsite = new JLabel("Website: ");
		lblWebsite.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblWebsite = new GridBagConstraints();
		gbc_lblWebsite.anchor = GridBagConstraints.WEST;
		gbc_lblWebsite.insets = new Insets(0, 25, 10, 5);
		gbc_lblWebsite.gridx = 0;
		gbc_lblWebsite.gridy = 8;
		panel.add(lblWebsite, gbc_lblWebsite);
		
		JLabel lblWeb = new JLabel(com.getCompanyWebsite());
		lblWeb.setFont(UtilityGUI.fontLblForm);
		lblWeb.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblWeb = new GridBagConstraints();
		gbc_lblWeb.anchor = GridBagConstraints.WEST;
		gbc_lblWeb.insets = new Insets(0, 5, 10, 5);
		gbc_lblWeb.gridx = 1;
		gbc_lblWeb.gridy = 8;
		panel.add(lblWeb, gbc_lblWeb);
		
		JLabel lblTotalEm = new JLabel("TS Nhân Viên :");
		lblTotalEm.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblTotalEm = new GridBagConstraints();
		gbc_lblTotalEm.anchor = GridBagConstraints.WEST;
		gbc_lblTotalEm.insets = new Insets(0, 25, 10, 5);
		gbc_lblTotalEm.gridx = 0;
		gbc_lblTotalEm.gridy = 9;
		panel.add(lblTotalEm, gbc_lblTotalEm);
		
		JLabel lblTotalEmNum = new JLabel(Integer.toString(com.getCompanyTotalEm()));
		lblTotalEmNum.setFont(UtilityGUI.fontLblForm);
		lblTotalEmNum.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblTotalEmNum = new GridBagConstraints();
		gbc_lblTotalEmNum.anchor = GridBagConstraints.WEST;
		gbc_lblTotalEmNum.insets = new Insets(0, 5, 10, 5);
		gbc_lblTotalEmNum.gridx = 1;
		gbc_lblTotalEmNum.gridy = 9;
		panel.add(lblTotalEmNum, gbc_lblTotalEmNum);
		
		JLabel lblTotalDep = new JLabel("TS phòng ban :");
		lblTotalDep.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblTotalDep = new GridBagConstraints();
		gbc_lblTotalDep.anchor = GridBagConstraints.WEST;
		gbc_lblTotalDep.insets = new Insets(0, 25, 10, 5);
		gbc_lblTotalDep.gridx = 0;
		gbc_lblTotalDep.gridy = 10;
		panel.add(lblTotalDep, gbc_lblTotalDep);
		
		JLabel lblTotalDepNum = new JLabel(Integer.toString(com.getCompanyTotalDep()));
		lblTotalDepNum.setFont(UtilityGUI.fontLblForm);
		lblTotalDepNum.setForeground(UtilityGUI.colorLblForm);
		GridBagConstraints gbc_lblTotalDepNum = new GridBagConstraints();
		gbc_lblTotalDepNum.anchor = GridBagConstraints.WEST;
		gbc_lblTotalDepNum.insets = new Insets(0, 5, 10, 5);
		gbc_lblTotalDepNum.gridx = 1;
		gbc_lblTotalDepNum.gridy = 10;
		panel.add(lblTotalDepNum, gbc_lblTotalDepNum);
		
		JLabel lblListDep = new JLabel("Phòng Ban :");
		lblListDep.setFont(UtilityGUI.fontHeader3);
		GridBagConstraints gbc_lblListDep = new GridBagConstraints();
		gbc_lblListDep.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblListDep.insets = new Insets(0, 25, 15, 5);
		gbc_lblListDep.gridx = 0;
		gbc_lblListDep.gridy = 11;
		panel.add(lblListDep, gbc_lblListDep);
		
		DefaultListModel model = new DefaultListModel();
		for(int i=0; i< listDep.size(); i++){
			String depId = Integer.toString(listDep.get(i).getDepId());
			String depName = (i+1)+". "+listDep.get(i).getDepName();
			model.add(i, new KeyValue(depId, depName));
		}
		jListDep = new JList(model);
		int total = listDep.size();
		UtilityGUI.styleJList(jListDep, total);
		jListDep.setBackground(getBackground());
		jListDep.addMouseListener(new MouseListDep());
		GridBagConstraints gbc_jListDep = new GridBagConstraints();
		gbc_jListDep.fill = GridBagConstraints.BOTH;
		gbc_jListDep.insets = new Insets(0, 5, 15, 5);
		gbc_jListDep.gridx = 1;
		gbc_jListDep.gridy = 11;
		panel.add(jListDep, gbc_jListDep);
		
		return panel;
	}
	
	
	private class MouseListDep extends MouseAdapter{
		public void mouseClicked(MouseEvent mouseEvent) {
			 if(mouseEvent.isControlDown() && mouseEvent.getButton() == mouseEvent.BUTTON1){
				 int depId = Integer.parseInt(((KeyValue)jListDep.getSelectedValue()).getKey());
				 int index = Main.jTabbedPane.positionTab(UtilityGUI.tabDepInfo);
				 if(index == -1){
					 Main.jTabbedPane.addTab(new Dep_View(depId), UtilityGUI.tabDepInfo);
				 	 Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				 }else{
					 Main.jTabbedPane.removeTabAt(index);
					 Main.jTabbedPane.addTab(new Dep_View(depId), UtilityGUI.tabDepInfo, index);
					 Main.jTabbedPane.setSelectedIndex(index);
				 }
			 }
		}	 
	} 

}
